<?php

namespace app\model\v1\recruit;

use crmeb\traits\ModelTrait;
use think\Model;

class RecruitJobPoints extends Model
{

    use ModelTrait;

    protected $hidden = [
        //     'create_time','update_time','delete_time'
    ];

    protected $updateTime = false;
    protected $createTime = false;




    /**
     * 求职招聘后台 - 积分配置管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getJobPointsList($param = [])
    {
        $data["data"] = self::field ("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("points",'like', '%'.$param["word"].'%');
            })
            ->order("id", "desc")
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("points",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }


    /**
     * 积分配置管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function createJobPoints($param = [])
    {
        $add = [
            "points"        => $param["points"],
            "phone_count"   => $param["phone_count"] ?? 0,
            "chat_count"    => $param["chat_count"] ?? 0,
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     * 积分配置管理列表 - 编辑
     * @param $param
     * @return RecruitIndustry
     */
    public static function updateJobPoints($param = [])
    {
        $update["update_time"] = time();
        if(!empty($param["points"])) $update["points"] = $param["points"];
        if(!empty($param["phone_count"])) $update["phone_count"] = $param["phone_count"];
        if(!empty($param["chat_count"])) $update["chat_count"] = $param["chat_count"];

        return self::where("id", $param["id"])->update($update);
    }





    public static function enableJobPoints($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }




}